#!/usr/bin/env python
# encoding: utf-8


"""
@file: mianjizuizhi.py
@time: 2016/12/7 上午10:16
"""
# 求面积最值
from mathsolver.functions.base import *
from bianjiaozhuanhuan import SIDES, ANGLES
from sympy import pi, sin, cos, tan, solveset
import re


# 转换【已知一组角与对边】求三角形面积的最大值
# 首先判断 目标边或者角。看是否有a = m, sin(A)|cos(A) = n 的形式
class MianJiZuiZhi001(BaseFunction):
    def solver(self, *args):
        self.label.add('三角形面积最值')
        eqs = args[0].sympify()
        target = ''
        du_p = r'du(\(\d+?\))'
        # ----查找目标边或者角变量
        for tmp_eq in eqs:
            l_expr, r_expr = tmp_eq
            tmp_f = l_expr - r_expr
            du_m = re.search(du_p, str(tmp_f))
            if du_m:
                du_str = du_m.group()
                du_sub = sympify(du_m.groups()[0]) * pi / 180
                tmp_f = tmp_f.subs(du_str, du_sub)
            symbs = tmp_f.free_symbols
            if len(symbs) == 1:
                symb = list(symbs)[0]
                if FiniteSet(symb).is_subset(SIDES):  # 如果是边的变量， 那么就可以确定边的选择了
                    target = symb
                    break
                elif FiniteSet(symb).is_subset(ANGLES):
                    symb_value = solveset(tmp_f)
                    if isinstance(symb_value, FiniteSet):  # 如果是类似 A = pi/4
                        target = symb
                        break
                    sin_symb_value = solve(tmp_f, sin(symb))
                    cos_symb_value = solve(tmp_f, cos(symb))
                    tan_symb_value = solve(tmp_f, tan(symb))
                    if sin_symb_value and sin_symb_value[0].is_real:
                        target = symb
                        break
                    elif cos_symb_value and cos_symb_value[0].is_real:
                        target = symb
                        break
                    elif tan_symb_value and tan_symb_value[0].is_real:
                        target = symb
                        break
                else:
                    pass
        if not target:
            raise Exception('try error')

        target_angle = sympify(str(target).upper())  # 目标角
        target_angle_value = ''
        target_side = sympify(str(target).lower())  # 目标边
        other_eq = ''
        for tmp_eq in eqs:
            l_expr, r_expr = tmp_eq
            tmp_f = l_expr - r_expr
            du_m = re.search(du_p, str(tmp_f))
            if du_m:
                du_str = du_m.group()
                du_sub = sympify(du_m.groups()[0]) * pi / 180
                tmp_f = tmp_f.subs(du_str, du_sub)
            symbs = tmp_f.free_symbols
            if len(symbs) == 1:
                symb = list(symbs)[0]
                if symb == target_side:
                    target_side_value = list(solveset(tmp_f))[0]
                elif symb == target_angle:  # 如果是角
                    symb_value = solveset(tmp_f)
                    if isinstance(symb_value, FiniteSet):  # 如果是类似 A = pi/4
                        target_angle_value = symb_value
            else:
                other_eq = tmp_f
        target_symb_subs = []
        if target_angle_value:  # 如果知道角度的值，那么sin, cos, tan就都知道了
            target_angle_sin_value = sin(target_angle_value)
            target_symb_subs.append((sin(target_angle), target_angle_sin_value))
            target_angle_cos_value = cos(target_angle_value)
            target_symb_subs.append((cos(target_angle), target_angle_cos_value))
            target_angle_tan_value = tan(target_angle_value)
            target_symb_subs.append((tan(target_angle), target_angle_tan_value))
        # other_eqs = JiaoHuaBian(verbose=True).solver(BaseEq([other_eq, '0'])).output[0].sympify()
        # tmp_eq = other_eqs[0][0] - other_eqs[0][1]
        return self


if __name__ == '__main__':
    pass
